home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / .bin / httpd / src / http_request.c < prev    next >
C/C++ Source or Header  |  1995-05-18  |  5KB  |  230 lines

  1. /*
  2.  * http_request.c: functions to get and process requests
  3.  * 
  4.  * All code contained herein is covered by the Copyright as distributed
  5.  * in the README file in the main directory of the distribution of 
  6.  * NCSA HTTPD.
  7.  *
  8.  * 03-21-95  Based on NCSA HTTPd 1.3 by Rob McCool
  9.  * 
  10.  * 04-19-95 blong
  11.  *    Forgot to remove free(remote_ip) from here, like elsewhere
  12.  *
  13.  * 04-20-95 blong
  14.  *    Added patch "B18" for redirects w/o delay by Robert Hartill
  15.  *
  16.  * 05-01-95 blong
  17.  *    Added patch by Steve Abatangle (sabat@enterprise.DTS.Harris.COM)
  18.  *    to log SIGPIPE and timed out differently in send_fd_timed_out
  19.  */
  20.  
  21.  
  22. #include "httpd.h"
  23. #include <setjmp.h>
  24. #include "new.h"
  25.  
  26. int assbackwards;
  27. char *remote_host = NULL;
  28. char *remote_ip = NULL;
  29. char *remote_name = NULL;
  30. FILE *time_out_fd;
  31. extern JMP_BUF jmpbuffer;
  32. extern int getline_seen_cmd;
  33. extern int getline_buffered_fd;
  34.  
  35. /* If RFC931 identity check is on, the remote user name */
  36. char *remote_logname;
  37. static void (*exit_callback)();
  38.  
  39. void send_fd_timed_out(int sigcode) {
  40.     char errstr[MAX_STRING_LEN];
  41.  
  42.     fclose(time_out_fd);
  43.     if(exit_callback) (*exit_callback)();
  44.     if (sigcode != SIGPIPE) {
  45.       sprintf(errstr,"httpd: send timed out for %s",remote_name);
  46.     } else {
  47.       sprintf(errstr,"httpd: send aborted for %s",remote_name);
  48.     }
  49.     log_error(errstr);
  50.     log_transaction();
  51.     if (in_headers_env) {
  52.     free_env(in_headers_env);
  53.     in_headers_env = NULL;
  54.     }
  55.     if (!standalone) {
  56.        fclose(stdin); 
  57.         fclose(stdout); 
  58.     exit(0);
  59.     } else {
  60.       if (remote_host) {
  61.     free(remote_host);
  62.     remote_host = NULL;
  63.       }    
  64. /* Duh.  Free memory in bss */
  65. /*      if (remote_ip) free(remote_ip); */
  66.       if (remote_name) {
  67.     free(remote_name);
  68.     remote_host = NULL;
  69.       }
  70. #if defined(NeXT) || defined(__mc68000__)
  71.       longjmp(jmpbuffer,1);
  72. #else
  73.       siglongjmp(jmpbuffer,1);
  74. #endif
  75.     }
  76. }
  77.  
  78. /*
  79.   We'll make it return the number of bytes sent
  80.   so that we know if we need to send a body by default
  81. */
  82. long send_fd(FILE *f, FILE *fd, void (*onexit)())
  83. {
  84.     char buf[IOBUFSIZE];
  85.     long total_bytes_sent;
  86.     register int n,o,w;
  87.  
  88.     time_out_fd = f;
  89.     exit_callback = onexit;
  90.     signal(SIGALRM,send_fd_timed_out);
  91.     signal(SIGPIPE,send_fd_timed_out); 
  92.  
  93.     total_bytes_sent = 0;
  94.     while (1) {
  95.         alarm(timeout);
  96.         if((n=fread(buf,sizeof(char),IOBUFSIZE,f)) < 1) {
  97.             break;
  98.         }
  99.         o=0;
  100.         if(bytes_sent != -1)
  101.             bytes_sent += n;
  102.         while(n) {
  103.             w=fwrite(&buf[o],sizeof(char),n,fd);
  104.             n-=w;
  105.             o+=w;
  106.         total_bytes_sent += w;
  107.         }
  108.     }
  109.     fflush(fd);
  110.     alarm(0);
  111.     signal(SIGALRM,SIG_IGN);
  112.     signal(SIGPIPE,SIG_IGN);
  113.     return total_bytes_sent;
  114. }
  115.  
  116. int find_script(char *method, char *name, char *args, int in, FILE *out) 
  117. {
  118.     int n=count_dirs(name),i;
  119.     char t[HUGE_STRING_LEN],ct_bak[MAX_STRING_LEN];
  120.     struct stat finfo;
  121.  
  122.     strcpy(ct_bak,content_type);
  123.     for(i=n;i;--i) {
  124.         make_dirstr(name,i,t);
  125.         probe_content_type(t);
  126.         if(!strcmp(content_type,CGI_MAGIC_TYPE)) {
  127.             char pa[HUGE_STRING_LEN];
  128.             int l=strlen(t);
  129.             
  130.             if(stat(t,&finfo) == -1)
  131.                 continue;
  132.             if(!(S_ISREG(finfo.st_mode)))
  133.                 return 0;
  134.             strcpy(pa,&name[l]);
  135.             name[l] = '\0';
  136.             strcpy(content_type,ct_bak);
  137.             send_cgi(method,name,pa,args,&finfo,in,out);
  138.             return 1;
  139.         }
  140.     }
  141.     return 0;
  142. }
  143.  
  144. char method[HUGE_STRING_LEN];
  145. char protocal[HUGE_STRING_LEN];
  146. char the_request[HUGE_STRING_LEN];
  147. char failed_request[HUGE_STRING_LEN];
  148. char as_requested[HUGE_STRING_LEN];
  149. char url2[HUGE_STRING_LEN];
  150. char failed_url[HUGE_STRING_LEN];
  151. char args2[HUGE_STRING_LEN];
  152.  
  153. void initialize_request() {
  154.  
  155.    /* reset security information to access config defaults */
  156.     reset_security();
  157.  
  158.    /* Initialize Error codes */ 
  159.     ErrorStat = 0;
  160.     status = 200;
  161.     
  162.     init_header_vars();
  163.     reset_to_saved_aliases();
  164.     exit_callback = NULL;
  165.     as_requested[0] = '\0';
  166.     failed_url[0] = '\0';
  167.     failed_request[0] = '\0';
  168.     local_default_type[0] = '\0';
  169.     local_default_icon[0] = '\0';
  170.  
  171. /* All but HEAD send more than a header */
  172.     header_only = 0;
  173.  
  174.     bytes_sent = -1;
  175.  
  176. /* Initialize buffered getline */
  177.     getline_buffered_fd = -1;
  178.     getline_seen_cmd = 0;
  179.  
  180. }
  181.  
  182.  
  183. void process_request(int in, FILE *out) {
  184.     get_remote_host(in);
  185.     signal(SIGPIPE,send_fd_timed_out); 
  186.  
  187.  
  188.     if(getline(as_requested,HUGE_STRING_LEN,in,timeout))
  189.         return;
  190.     if(!as_requested[0]) 
  191.         return;
  192.  
  193.     strcpy(the_request, as_requested);
  194.  
  195.     getword(method,as_requested,' ');
  196.     getword(args2,as_requested,' ');
  197.     getword(url2,args2,'?');
  198.  
  199.     unescape_url(url2);
  200.     getword(protocal,as_requested,'\0');
  201.  
  202.     if(protocal[0] != '\0') {
  203.         assbackwards = 0;
  204.         get_mime_headers(in,out, url2);
  205.     }
  206.     else
  207.         assbackwards = 1;
  208.  
  209.     if(!strcmp(method,"HEAD")) {
  210.         header_only=1;
  211.         process_get(in,out,method,url2,args2);
  212.     }
  213.     else if(!strcmp(method,"GET")) {
  214.         process_get(in,out,method,url2,args2);
  215.     }
  216.     else if(!strcmp(method,"POST")) {
  217.         post_node(url2,args2,in,out);
  218.     }
  219.     else if(!strcmp(method,"PUT")) {
  220.         put_node(url2,args2,in,out);
  221.     }
  222.     else if(!strcmp(method,"DELETE")) {
  223.         delete_node(url2,args2,in,out);
  224.     }
  225.     else 
  226.         die(BAD_REQUEST,"Invalid or unsupported method.",out);
  227.  
  228. }
  229.  
  230.